import api from '@/server/api'
import Base from '@/views/Base'
const {login: loginRequest, register: registerRequest} = api

export default class extends Base {
  async login () {
    const {username, password} = this.vm.loginInfo
    if (!username || !password) {
      this.vm.$Message.error('用户名或密码为空')
      return
    }
    return new Promise(async (resolve, reject) => {
      const res = await loginRequest(username, password)
      if (res.status === 0) {
        this.vm.$store.commit('SET_USER', res.result)
        this.vm.$Message.success('登录成功')
        this.vm.$router.push('/home')
      } else {
        this.vm.loginInfo.password = ''
        this.vm.$Message.error('登录失败')
      }
    })
  }
  async register () {
    const {username, password} = this.vm.registerInfo
    if (!username || !password) {
      this.vm.$Message.error('用户名或密码为空')
      return
    }
    const res = await registerRequest(username, password)
    this.vm.$router.push('/home')
    this.vm.$Message.success(res.msg)
  }
}
